package com.arvato.minipro.service.mall;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.arvato.minipro.bean.bean_do.mall.CommodityCutDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_vo.mall.CommodityCutVo;
import com.arvato.minipro.mapper.mall.CommodityCutMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @author: smallking
 * 
 * @Descibe: 处理砍价商品的Service
 * 
 * @Date: 2018年7月4日  下午4:30:57
 */
@Service
@Transactional
public class CommodityCutService {
	
	@Autowired
	private CommodityCutMapper commodityCutMapper;
	
	@Autowired
	private CommodityService commodityService; 
	
	/**
	 * 根据主键ID查询一个砍价商品的详情
	 * @param commodityCutId
	 * @return
	 */
	public CommodityCutVo getCommodityCutById(String commodityCutId) {
		CommodityCutVo commodityCutVo = commodityCutMapper.getCommodityCutById(commodityCutId);
		commodityCutVo.setCommodityVo(commodityService.getCommodityById(commodityCutVo.getCommodityId()));
		return commodityCutVo;
	}
	/**
	 * 根据商品ID查询一个商品当前的的砍价规则
	 * @param commodityId
	 * @return
	 */
	public CommodityCutVo getCommodityCutByCommodityId(String commodityId) {
		Query query = new Query();
		query.put("commodityId", commodityId);
		query.put("status", 1);
		List<CommodityCutVo> commodityCutList = commodityCutMapper.listCommodityCutByQuery(query);
		if(commodityCutList.size() >= 1) {
			return commodityCutList.get(0);
		}
		return null;
	}
	
	/**
	 * 根据Query 多条件查询砍价商品
	 * @param query
	 * @return
	 */
	public PageData listCommodityCutByQuery(Query query) {
		// 开启分页 设置参数
		PageHelper.startPage(query.getPageNum(), query.getPageSize());
		// 开始查询
		List<CommodityCutVo> list = commodityCutMapper.listCommodityCutByQuery(query);
		// 得到分页结果
		PageInfo<CommodityCutVo> pageInfo = new PageInfo<CommodityCutVo>(list);
		List<CommodityCutVo> resultList = pageInfo.getList();
		for(CommodityCutVo commodityCutVo : resultList) {
			commodityCutVo.setCommodityVo(commodityService.getCommodityById(commodityCutVo.getCommodityId()));
		}
		return new PageData(pageInfo.getTotal(), resultList);
	}
	
	/**
	 * 新增一个砍价商品
	 * @param commodityCutDo
	 * @return
	 */
	public int insert(CommodityCutDo commodityCutDo) {
		return commodityCutMapper.insert(commodityCutDo);
	}
	
	/**
	 * 修改一个砍价商品
	 * @param commodityCutDo
	 * @return
	 */
	public int update(CommodityCutDo commodityCutDo) {
		return commodityCutMapper.update(commodityCutDo);
	}
	
	/**
	 * 检查一个商品是否已经被添加到了砍价
	 * @param commodityId
	 * @return
	 */
	public boolean checkCommodityCut(String commodityId) {
		CommodityCutVo commodityCutVo = getCommodityCutByCommodityId(commodityId);
		if(commodityCutVo != null) {
			return true;
		}
		return false;
	}

	
	
	
	
}
